import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

// ============ Ceye 相关 API ============

/** 获取 Ceye 配置列表 */
export const getCeyeConfigs = () => {
  return taskRequest<Result>("get", "/api/ceye/configs");
};

/** 获取统计数据 */
export const getCeyeStatistics = (data?: { config_name?: string }) => {
  return taskRequest<Result>("post", "/api/ceye/statistics", {
    data,
  });
};

/** 清空记录 */
export const clearCeyeRecords = (data?: {
  config_name?: string;
  confirm?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/ceye/records/clear", {
    data,
  });
};

/** 测试连接 */
export const testCeyeConnection = (data?: { config_name?: string }) => {
  return taskRequest<Result>("post", "/api/ceye/test", {
    data,
  });
};

/** 查询 DNS 记录 */
export const getDNSRecords = (data?: {
  config_name?: string;
  filter?: string;
  page?: number;
  page_size?: number;
}) => {
  return taskRequest<Result>("post", "/api/ceye/dns/records", data);
};

/** 查询 HTTP 记录 */
export const getHTTPRecords = (data?: {
  config_name?: string;
  filter?: string;
  page?: number;
  page_size?: number;
}) => {
  return taskRequest<Result>("post", "/api/ceye/http/records", data);
};

/** 搜索全部记录 */
export const searchAllRecords = (data?: {
  config_name?: string;
  keyword?: string;
  type?: "all" | "dns" | "http";
  page?: number;
  page_size?: number;
}) => {
  return taskRequest<Result>("post", "/api/ceye/records/search", data);
};
